package com.hengtuo.kaiyuanzhongguo;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hengtuo.adapter.FinderAdapter;
import com.hengtuo.vo.FinderName;
import com.hengtuo.vo.Usered;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.thoughtworks.xstream.XStream;

import java.util.ArrayList;
import java.util.List;

/**
 * 找人    http://www.oschina.net/action/api/find_user?name=
 */
public class FinderActivity extends Activity implements View.OnClickListener {

    private ImageView back;
    private EditText name;
    String edit;
    private ImageView clean;
    private ListView listview;
    private FinderAdapter adapter;
    private HttpUtils utils;
    private List<Usered> list = new ArrayList<>();
    private String URL = "http://www.oschina.net/action/api/find_user?name=";
    private ProgressBar progressBar;
    private TextView progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finder);

        initialize();


    }


    private void getHttp(String ed) {

        utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, URL + ed, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String str = responseInfo.result;
                XStream xStream = new XStream();
                xStream.processAnnotations(FinderName.class);
                FinderName finderName = new FinderName();
                finderName = (FinderName) xStream.fromXML(str);
                list = finderName.getUsers().getUser();
                progressBar.setVisibility(View.GONE);
                progress.setVisibility(View.GONE);
                adapter = new FinderAdapter(FinderActivity.this, list);
                listview.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.finder_back:
                finish();
                break;

            case R.id.finder_clean:
            name.setText("");
                break;

        }
    }

    private void initialize() {

        back = (ImageView) findViewById(R.id.finder_back);
        back.setOnClickListener(this);
        name = (EditText) findViewById(R.id.finder_name);
        clean = (ImageView) findViewById(R.id.finder_clean);
        clean.setOnClickListener(this);
        listview = (ListView) findViewById(R.id.finder_listview);
        progress = (TextView) findViewById(R.id.finder_progress);
        progressBar = (ProgressBar) findViewById(R.id.finder_cicle_progressbar);


        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                clean.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.VISIBLE);
                progress.setVisibility(View.VISIBLE);


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                edit = name.getText().toString();
                if (edit.equals("")){
                    return;
                }else {
                    getHttp(edit);
                }
            }
        });
    }
}
